
package org.dkakunsi.aplikasitoko.form.kasir;

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import org.dkakunsi.aplikasitoko.data.entity.Kasir;
import org.dkakunsi.aplikasitoko.data.entity.Log;
import org.dkakunsi.aplikasitoko.data.util.ApplicationDataException;
import org.dkakunsi.aplikasitoko.form.Desktop;
import org.dkakunsi.aplikasitoko.form.MyForm;
import org.dkakunsi.aplikasitoko.service.DataService;

public class Password extends MyForm {

	private static final long serialVersionUID = 1L;

	private JButton btnOK;
	private JLabel labelPass;
	private JLabel labelPass2;
	private JPasswordField textFieldPass;
	private JPasswordField textFieldPass2;

	/**
	 * create frame with no desktop initialized
	 * @param judul
	 */
	public Password(String judul) {
		super(judul);
		this.setter();
	}

	/**
	 * create frame with desktop initialized
	 * @param judul
	 * @param desktop
	 */
	public Password(String judul, Desktop desktop) {
		super(judul, desktop);
		this.setter();
	}
	
	private void setter(){
		this.setUkuran(335, 200);
		this.setBtnExit(215, 130);
		this.setUnableTable();

		labelPass = new JLabel("New Password");
		labelPass.setBounds(10, 25, 150, 25);
		getContentPane().add(labelPass);

		labelPass2 = new JLabel("Retype Password");
		labelPass2.setBounds(10, 65, 150, 25);
		getContentPane().add(labelPass2);
		
		textFieldPass = new JPasswordField();
		textFieldPass.setBounds(145, 25, 170, 25);
		getContentPane().add(textFieldPass);
		
		textFieldPass2 = new JPasswordField();
		textFieldPass2.setBounds(145, 65, 170, 25);
		getContentPane().add(textFieldPass2);
		
		btnOK = new JButton("OK");
		btnOK.setBounds(105, 130, 100, 25);
		getContentPane().add(btnOK);
		btnOK.setActionCommand("OK");
		btnOK.addActionListener(this);
		
		ds = new DataService();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		
		switch(command){
			case "EXIT" : exit();
				break;
			case "OK" : saveOk();
				break;
			default :
				break;
		}
	}

	@SuppressWarnings("deprecation")
	private void saveOk(){
		String PASS = textFieldPass.getText();
		String PASS2 = textFieldPass2.getText();
		
		if(!PASS.equals(PASS2)){
			JOptionPane.showMessageDialog( this, "Password tidak match", "Kesalahan Input", JOptionPane.ERROR_MESSAGE );
		} else {
			Log log = (Log) getParentForm().getPelaku();
			Kasir kasir = (Kasir) log.getKasir();
			ds.attach(kasir);
			kasir.setPass(PASS);
			try {
				ds.update(kasir);
				JOptionPane.showMessageDialog( this, "Password telah diganti", "Pemberitahuan", JOptionPane.DEFAULT_OPTION );
			} catch (ApplicationDataException e) {
				JOptionPane.showMessageDialog( this, "Call the Developer", "Application Error", JOptionPane.ERROR_MESSAGE );
//				e.printStackTrace();
			}
		}
	}


	@Override
	@Deprecated
	protected void loadInitialize() {
		// TODO Auto-generated method stub
		
	}

	@Override
	@Deprecated
	protected void loadView() {
		// TODO Auto-generated method stub
		
	}
}
